/**
 * Created by yangfan on 2016/10/12.
 */
define(['config/appregister',"i18n","business/personMine/services/httpServices"],function(app,i18n,httpService){
    app.controller('myDownloadCtrl', function($scope,$state) {
        $scope.i18n = i18n;
        $scope.everPageShow = [30,50,100];
        $scope.tableTitle = ["资料标题","资料分类","消耗(积分)","下载日期"];

        // 切换title
        $scope.isDataChecked = true;
        $scope.isThesisChecked = false;
        $scope.checkTitle = function(type){
            $scope.isCurrent = [];
            $scope.isCurrent[0] = "bold";

            $scope.listParams.pageNo = 1;
            $scope.listParams.pageSize = 10;
            $scope.listParams.itemsPerPage = 10;
            $scope.listParams.numberOfPages = parseInt($scope.totalItem/$scope.listParams.pageSize);
            //  $scope.$broadcast("numberOfPagesChange",{});
            initData(type) ;
        };
        function initData(type){
            if(type == "data"){
                $scope.isDataChecked = true;
                $scope.isThesisChecked = false;

                myDownd();
            }else if(type == "thesis"){
                $scope.isThesisChecked = true;
                $scope.isDataChecked = false;
                var req ={
                    type:"GET",
                    uri:"myDownLoad",
                    data:{
                        pageNo:$scope.listParams.pageNo,
                        pageSize:$scope.listParams.pageSize,
                        keyWord:$scope.listParams.keyWord
                    }
                };
                httpService.getLibraryUploadFile(req).success(function(response){
                    $scope.$apply(function(){
                        console.log(response.data.list)
                        $scope.myDownload = response.data.list;
                        //todo 待分类 国内国际
                        //todo 待分类 国内国际
                        $scope.myDownload.ecoin = response.data.list.downloadCount*response.data.list.price;
                        $scope.totalItem = response.data.Pagination.totalCount;
                        if($scope.totalItem == 0){
                            $scope.noContent = "您暂时没有下载的论文";
                        }
                        getPage();
                    });
                }).error(function(){
                    console.log("访问出错");
                })
            }
        }
        $scope.search = function(){
            if( $scope.isThesisChecked == true){
                type = "thesis";
            }else {
                type = "data";
            }
            $scope.checkTitle(type);
        };

        $scope.isCurrent = [];
        $scope.isCurrent[0] = "bold";
        $scope.listParams = {
            pageNo:1,
            pageSize:10,
            keyWord:""
        };
        //shit  code !
        function getPage(){
            $scope.paginationConf = {
                currentPage: $scope.listParams.pageNo,
                totalItems: $scope.totalItem,
                //每页预览条数
                itemsPerPage: $scope.listParams.pageSize,
                //分页总数
                numberOfPages:Math.ceil($scope.totalItem/$scope.listParams.pageSize),
                // 实际分页长度
                pagesLength:5,
                perPageOptions: [10, 20, 30, 40, 50],
                onChange: function () {
                    $scope.list = [];
                    $scope.listParams.pageNo = $scope.paginationConf.currentPage;
                    var type ;
                    if( $scope.isThesisChecked == true){
                        type = "thesis";
                    }else {
                        type = "data";
                    }
                    initData(type)
                   // myDownd();
                }
            };
            $scope.everPageShow = $scope.paginationConf.perPageOptions;
        };
        function myDownd(){
            var req = {
                type:"GET",
                data:{
                    pageNo:$scope.listParams.pageNo,
                    pageSize:$scope.listParams.pageSize,
                    keyWord:$scope.listParams.keyWord
                    /* all:false*/
                },
                uri:"myDownLoadFile"//黄军说的用我的下载的数据"myDownLoadFile"
            };
            httpService.getMyDownLoadFile(req).success(function(response){
                $scope.$apply(function(){
                    $scope.myDownload = response.data.list;
                    //todo 待分类 国内国际
                    $scope.myDownload.ecoin = response.data.list.downloadCount*response.data.list.price;
                    $scope.totalItem = response.data.Pagination.totalCount;
                    if($scope.totalItem == 0){
                    	$scope.noContent = "您暂时没有下载的资料";
                    }
                    getPage();
                })
            }).error(function(){
                console.log("访问出错");
            })
        }
        myDownd();

        //改变每页预览条数
        $scope.changeItemsPerPage = function(item ,index){
            $scope.isCurrent = [];
            $scope.isCurrent[index] = "bold";

            $scope.listParams.pageNo = 1;
            $scope.listParams.pageSize = item;
            $scope.paginationConf.itemsPerPage = item;
            $scope.paginationConf.numberOfPages = parseInt($scope.totalItem/$scope.listParams.pageSize);
            //  $scope.$broadcast("numberOfPagesChange",{});
            myDownd();
        }
        
        
        //跳转详情页
		$scope.goDetail = function(item){
			var state,param;
			console.log(item)
			if(item.thesisId){
				state = 'eng.libraryDetail';
				param = {thesisId:item.thesisId};
			}
			if(item.fileId){
				state = 'eng.techdetail';
				param = {fileId:item.fileId};
			}
			$state.go(state,param)
		}
    })
});